#include "AnimationPayload.h"

#include <SEDebug/Log/Log.h>

namespace SEngine
{
    AnimationPayload::AnimationPayload()
    {
    }

    void AnimationPayload::Start(int ms)
    {
        if (!m_entry.expired()) {
            m_fsm.Start(*m_entry.lock(), ms);
        }
    }

    void AnimationPayload::Stop()
    {
        m_fsm.Stop();
    }

    void AnimationPayload::RemoveState(std::function<bool(AnimationState &)> &&fn)
    {
        std::optional<std::reference_wrapper<AnimationState>> deletedState;
        ForeachState([&](AnimationState & state) {
            if (fn(state)) {
                deletedState = state;
            }
        });
        if (deletedState) {
            m_fsm.RemoveState(deletedState->get());
        }
    }

    void AnimationPayload::RemoveStateEvent(std::function<bool(StateEvent &)> &&fn)
    {
        m_events.erase(std::remove_if(m_events.begin(), m_events.end(), [&](std::unique_ptr<StateEvent> & e) { return fn(*e); }), m_events.end());
    }
    
    void AnimationPayload::NextState()
    {
        for (auto & event : m_events) {
            if (event->Transform()) {
                break;
            }
        }
    }
}